package com.ww.httpclient;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;

public class HttpClientPoolDemo {

    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("https://www.taobao.com");
        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

        /**
         * 连接对象被response对象持有，以保证内容通过response对象进行消费
         */

        try {
            System.out.println(httpResponse.getCode());
            HttpEntity responseEntity = httpResponse.getEntity();
            EntityUtils.consume(responseEntity);
        } finally {
            httpResponse.getCode();
        }

        /**
         * 这个sample设计很简单，几乎看不到任何连接池操作的蛛丝马迹
         *
         * 常规连接池注意点
         * 1、Connection的get/release配对；
         * 2、保证一次http交互中请求、响应处理完整干净；
         *
         * 比如一次请求交互中，因某种原因没有消费掉响应内容，导致该内容还处于socket的缓存中，继而使得同一个连接下的第二次交互响应内容为第一次的响应结果
         * 这种后果很可怕
         */
    }
}
